package com.zyg.servicebase.exectionhandler;


import com.zyg.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        e.printStackTrace();
        return R.error().message("执行了全局异常处理..");

    }

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException arithmeticException) {
        arithmeticException.printStackTrace();
        return R.error().message("执行了ArithmeticException异常处理");
    }


    //自定义异常
    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public R error(GuliException guliException) {
        log.error(guliException.getMessage());
        guliException.printStackTrace();
        return R.error().code(guliException.getCode()).message(guliException.getMsg());
    }
}
